package com.xudow.app.ui.task;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.activeshare.edu.ucenter.common.messages.Message;
import com.activeshare.edu.ucenter.common.messages.cart.CartMessage;
import com.activeshare.edu.ucenter.models.cart.ShoppingCartWithCourse;
import com.google.gson.Gson;
import com.xudow.app.Config;
import com.xudow.app.XApplication;
import com.xudow.app.util.rest.XRestTemplate;

import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.HashMap;
import java.util.List;

import static com.xudow.app.util.LogUtils.LOGE;
import static com.xudow.app.util.LogUtils.makeLogTag;

/**
 * 修改课程学生Task
 *
 * @author lukecheng
 */
public class CartStudentChangeTask extends AsyncTask<HashMap<String, String>, Void, Void> {
    private static final String TAG = makeLogTag(CartStudentChangeTask.class);

    private static final String ORDER_CREATE_URL = Config.BASE_URL + Config.SHOPPIN_CART_CHANGE_STUDENT_URL;

    public static final int SUCCESS = 0;
    public static final int ERROR = 1;

    private Context context;
    private Handler handler;

    public CartStudentChangeTask(Context context, Handler handler) {
        this.context = context;
        this.handler = handler;
    }

    private void sendErrorMessage(String msg) {
        android.os.Message message = new android.os.Message();
        message.what = ERROR;

        if (null != msg) {
            Bundle data = new Bundle();
            data.putString("errMessage", msg);
            message.setData(data);
        }

        handler.sendMessage(message);
    }

    private void sendSuccessMessage(Object obj) {
        android.os.Message message = new android.os.Message();
        message.what = SUCCESS;
        handler.sendMessage(message);
    }

    @Override
    protected Void doInBackground(HashMap<String, String>... parameters) {
        XApplication application = (XApplication) context.getApplicationContext();

        XRestTemplate restTemplate = new XRestTemplate(application.getCookieStore(), context);
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        //restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
        MultiValueMap<String, String> valueMap = new LinkedMultiValueMap<String, String>();

        valueMap.add("item_id", parameters[0].get("item_id"));
        valueMap.add("studentId", parameters[0].get("studentId"));
     //   Log.i("CartStudentChangeTask", "item_id>>" +  parameters[0].get("item_id")+"studentId>>>"+parameters[0].get("studentId"));
        try {
            CartMessage result = restTemplate.postForObject(ORDER_CREATE_URL, valueMap,
                    CartMessage.class);
//            Log.i("CartStudentChangeTask","string>>"+string);


            //String result=restTemplate.postForObject(ORDER_CREATE_URL, valueMap,String.class);
            if (result != null) {
//                Log.e(TAG, "doInBackground: "+result );
                if (result.getResult().equals("0")) {
                    sendSuccessMessage(result.getCartItems());
                } else if (result.getResult().equals("1")) {
                    sendErrorMessage(result.getErrorMessage());
                }
            } else {
                sendErrorMessage(null);
            }
        } catch (Exception e) {
            LOGE(TAG, e.getMessage());

            sendErrorMessage(e.getMessage());
        }

        return null;
    }
}
